import { Config } from 'config.js';

class Token {
  constructor(){
    this.verifyUrl = Config.restUrl + 'token/verify';
    this.tokenUrl = Config.restUrl + 'token/user';
  }

  verify(){
    var token = wx.getStorageSync('token');
    if(!token){
      this.getTokenFromServer();
    }else{
      this._verifyFromServer(token);
    }

  }

  /**携带令牌去服务器校验令牌 */
  _verifyFromServer(token){
    var that = this;
    wx.request({
      url: that.verifyUrl,
      method: 'POST',
      data :{
        token:token
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success:function(res){
        var valid = res.data.isvalid;
        console.log(valid);
        if (!valid){
          that.getTokenFromServer();
        }
      }
    })
  }


  /**从服务器获取token */
  getTokenFromServer(callBack){
    var that = this;
    wx.login({
      success:function(res){
        console.log(res);
        wx.request({
          url: that.tokenUrl,
          method : 'POST',
          data : {
            code : res.code
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success:function(res){
            wx.setStorageSync('token', res.data.token);
            callBack && callBack(res.data.token);
          }
        })
      }
    })
  }


}
export { Token };